package cn.lyh.qinfeng.plugin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.lyh.qinfeng.pojo.Memberinfo;
import cn.lyh.qinfeng.service.BaoMingService;
import net.lz1998.cq.event.message.CQGroupMessageEvent;
import net.lz1998.cq.robot.CQPlugin;
import net.lz1998.cq.robot.CoolQ;

/**
 * @author lyh
 * @version 1.0
 * @date 2020/3/20
 * @Description 团队信息查询插件，用于团队信息的查询
 */
@Component
public class GetTeamInfoPlugin extends CQPlugin {
    @Autowired
    BaoMingService baoMingService;

    /**
     * Q群团队信息查询指令插件，用于监控Q群发送的信息
     *
     * @param cq    机器人对象
     * @param event 事件对象
     * @return 是否执行下一个插件，并将查询信息返回给Q群
     */
    @Override
    public int onGroupMessage(CoolQ cq, CQGroupMessageEvent event) {
        String msg = event.getMessage().trim();
        System.out.println("getTeammsg = " + msg);
        long groupId = event.getGroupId();

        Map<String, Map<String, String>> rstMap = new HashMap<>();

        int neiNo = 0;
        int waiNo = 0;
        int tNo = 0;
        int naiNo = 0;
        int sxNo = 0;
        Map<String, String> neigongMap = new HashMap<>();
        Map<String, String> waigongMap = new HashMap<>();
        Map<String, String> naimaMap = new HashMap<>();
        Map<String, String> tMap = new HashMap<>();
        Map<String, String> sxMap = new HashMap<>();

        //查看团队信息 1
        // [内功]
        if (msg.contains("查看信息")) {
            //拿到团队ID然后通过团队ID作为条件去查询数据
            Integer tuanId = Integer.valueOf(msg.split(" ")[1]);
            List<Memberinfo> memberinfoList = baoMingService.getAllInfo(tuanId);
            for (Memberinfo memberinfo : memberinfoList) {
                if (memberinfo.getMebclass().equals("内功")) {
                    neiNo++;
                    neigongMap.put(memberinfo.getNickname(), memberinfo.getMeboccupation());
                } else if (memberinfo.getMebclass().equals("外功")) {
                    waiNo++;
                    waigongMap.put(memberinfo.getNickname(), memberinfo.getMeboccupation());
                } else if (memberinfo.getMebclass().equals("T")) {
                    tNo++;
                    tMap.put(memberinfo.getNickname(), memberinfo.getMeboccupation());
                } else if (memberinfo.getMebclass().equals("奶妈")) {
                    naiNo++;
                    naimaMap.put(memberinfo.getNickname(), memberinfo.getMeboccupation());
                } else if (memberinfo.getMebclass().equals("双休奶")) {
                    sxNo++;
                    sxMap.put(memberinfo.getNickname(), memberinfo.getMeboccupation());
                }
            }
            rstMap.put("内功", neigongMap);
            rstMap.put("外功", waigongMap);
            rstMap.put("T", tMap);
            rstMap.put("奶", naimaMap);
            rstMap.put("双休奶", sxMap);
            /**
             * 内功:neigongmap
             *
             * 外功:waigongmap
             *
             * T奶
             */
            tNo += naiNo;
            String rstStr = "内功:" + neigongMap + "共:" + neiNo + "\n"
                    + "外功:" + waigongMap + "共:" + waiNo + "\n"
                    + "T奶:" + tMap + naimaMap + "共:" + tNo + "\n"
                    + "双休:" + sxMap;
            cq.sendGroupMsg(groupId,
                    rstStr,
                    false);
            return MESSAGE_BLOCK;
        }
        return MESSAGE_IGNORE;
    }
}
